package com.machine.action.component;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.Componentpart;
import com.machine.bean.PageBean;
import com.machine.service.user.ComponentServiceDao;
import com.opensymphony.xwork2.ActionSupport;

@Component("findHasOutAction")
@Scope(value = "prototype")
public class FindHasOutAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ComponentServiceDao componentServiceDao;
	private List<Componentpart> componentparts = null;
	private String username;

	private int page; // 第几页

	private PageBean pageBean; // 包含分布信息的bean

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public List<Componentpart> getComponentparts() {
		return componentparts;
	}

	public void setComponentparts(List<Componentpart> componentparts) {
		this.componentparts = componentparts;
	}

	public ComponentServiceDao getComponentServiceDao() {
		return componentServiceDao;
	}

	@Resource(name = "componentService")
	public void setComponentServiceDao(ComponentServiceDao componentServiceDao) {
		this.componentServiceDao = componentServiceDao;
	}

	@Override
	public String execute() throws Exception {
		if (page <= 0) {
			page = 1;
		}
		pageBean = componentServiceDao.queryForPage(5, page,username);
		componentparts = pageBean.getList();
		System.out.println(username);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out
				.print("<form method='post' class='outinfoform' ><table class='back_table' cellspacing='0' cellpadding='0'>");
		out
				.print("<tr><th width='30'>选择</th><th>名称</th><th>价格</th><th>库存数量</th><th>发布时间</th><th>发布状态</th></tr>");

		for (int i = 0; i < componentparts.size(); i++) {
			out.print("<tr>");
			out
					.print("<th width='20'><input name='componentpartsId' value='"
							+ componentparts.get(i).getId()
							+ "' type='checkbox'></th>");
			out.print("<th>");
			out.print(componentparts.get(i).getName());
			out.print("</th>");
			/*out.print("<th>");
			out.print(componentparts.get(i).getSpecification());
			out.print("</th>");
			out.print("<th>");
			out.print(componentparts.get(i).getMaterialquality());
			out.print("</th>");
			out.print("<th>");
			out.print(componentparts.get(i).getWeight());
			out.print("</th>");
			out.print("<th>");
			out.print(componentparts.get(i).getStandard());
			out.print("</th>");
			out.print("<th>");
			out.print(componentparts.get(i).getQualityclass());
			out.print("</th>");
			*/
			out.print("<th>");
			out.print(componentparts.get(i).getPrice());
			out.print("</th>");
			
			out.print("<th>");
			out.print(componentparts.get(i).getStockamount());
			out.print("</th>");
			out.print("<th>");
			out.print(componentparts.get(i).getOuttime());
			out.print("</th>");
			out.print("<th>");
			out.print(componentparts.get(i).getOutstate());
			out.print("</th>");
			out.print("</tr>");
		}

		out.print("<tr>");
		out.print("<td>");
		if(pageBean.getCurrentPage() == 1){
			out.print("上一页	首页");
		}else{
			out
			.print("<a onclick='return changeframe(1);'>首页</a>");

			out.print("<a onclick='return changeframe("+(pageBean.getCurrentPage() - 1)+");' >上一页</a>");
		}

		
		out.print("</td>");
		out.print("<td>");
		if(pageBean.getCurrentPage() != pageBean.getTotalPage()){
		
		out.print("<a onclick='return changeframe("+(pageBean.getCurrentPage() + 1)+");' >下一页</a>");

		out.print("<a onclick='return changeframe("+pageBean.getTotalPage()+");'>尾页</a>");
		}else{
			out.print("下一页	尾页");
		}

		out.print("共" + pageBean.getAllRow() + "条记录");
		out.print("</td>");
		out.print("</tr>");
		out.print("</table>");
		out.print("</form>");
		out.flush();
		out.close();

		return SUCCESS;
	}

}
